class_name CampSystem
extends BaseSystem

#region 属性
@export var camp_infos:Array[CampInfo]
var camp_info_dict:Dictionary
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
func init_system(args = null):
	super(args)
	camp_info_dict = {}
	for info in camp_infos:
		if not camp_info_dict.has(info.camp):
			camp_info_dict[info.camp] = info
#endregion
#region 公共方法
func get_target(camps:Array[GameEnum.Camp],pos:Vector2 = Vector2.ZERO,is_sort:bool = false,count:int = -1,radius:float = 0) -> Array[Node2D]:
	var targets:Array[Node2D] = []
	for camp in camps:
		var group_name:String = "Camp_" + GameEnum.Camp.keys()[camp]
		if get_tree().has_group(group_name):
			for node in get_tree().get_nodes_in_group(group_name):
				if not targets.has(node):
					targets.append(node)
	if targets.size() > 0:
		if is_sort:
			targets.sort_custom(func (a,b):return (a.global_position - pos).length() < (b.global_position - pos).length())
		if radius > 0:
			targets = targets.filter(func (x):return (x.global_position - pos).length() <= radius)
		count = targets.size() if count < 0 else count
		return targets.slice(0,count)
	else:
		return []
func group_add(camp:GameEnum.Camp, item:Node):
	var group_name:String = "Camp_" + GameEnum.Camp.keys()[camp]
	if not item.is_in_group(group_name):
		item.add_to_group(group_name)
func group_remove(camp:GameEnum.Camp,item:Node):
	var group_name:String = "Camp_" + GameEnum.Camp.keys()[camp]
	if item.is_in_group(group_name):
		item.remove_from_group(group_name)
func get_camp_info(camp:GameEnum.Camp) -> CampInfo:
	return camp_info_dict.get(camp)
func get_target_camps(camp:GameEnum.Camp) -> Array[GameEnum.Camp]:
	if camp_info_dict.has(camp):
		return get_camp_info(camp).target_camps
	return []
func get_friend_camps(camp:GameEnum.Camp) -> Array[GameEnum.Camp]:
	if camp_info_dict.has(camp):
		return get_camp_info(camp).friend_camps
	return []
#endregion
#region 私有方法
#endregion
#region 生命周期函数
#endregion
